﻿using CommonConfig;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace WXDataServer
{
    public class DataManager
    {
        /// <summary>
        /// 根据Openid获取用户信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public static DataTable GetUserByOpenId(string openid)
        {
            SqlHelper sqlHelper = new SqlHelper();
            DataTable dataTable = new DataTable();
            string sqlStr = "select * from SYS_User where Openid = '" + openid + "'";
            DataSet dataSet = sqlHelper.ExecuteDataSet("SYS_User", sqlStr);
            if (dataSet.Tables.Count > 0)
                dataTable = dataSet.Tables[0];
            return dataTable;
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="openid">openid</param>
        /// <param name="name">姓名</param>
        /// <param name="sex">性别</param>
        /// <param name="avatarurl">头像</param>
        /// <param name="message">返回信息</param>
        /// <returns></returns>
        public static bool Register(string openid, string name, string sex, string avatarurl,out string message,out DataRow dataRow)
        {
            SqlHelper sqlHelper = new SqlHelper();
            bool result = false;
            message = "";
            Guid userId = Guid.NewGuid();
            string sqlStr = "insert into SYS_User(Id,OpenId,DtoreId,Name,Sex,Job,AvatarUrl,CreateTime,Enable) values('" + userId
                + "','"+ openid + "','" + Guid.NewGuid() + "','" + name + "','" + sex + "','" + "未设置" + "','"+ avatarurl 
                + "','" + DateTime.Now.ToString(Propertys.DataFormat) + "','" + 1 + "')";
            int count = sqlHelper.ExecuteNonQuery(sqlStr, out message);
            if (count == 1)
            {
                result = true;
                sqlStr = "select * from SYS_User where Id = '"+ userId+"'";
                DataTable dataTable = sqlHelper.ExecuteDataSet("userTable", sqlStr).Tables[0];
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    dataRow = dataTable.Rows[0];
                }
                else
                {
                    dataRow = null;
                }
            }
            else
            {
                dataRow = null;
            }
            return result;
        }
    }
}
